﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CommonHelp;
using Core.Unity;
using Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Services;
using ViewModels;

namespace Commands {
    /// <summary>
    /// 获取水雨情信息
    /// </summary>
    public class QuerySYQCommand : CommandBase {
        private IXContainer _hook;
        private IDisplayTable _IDisplayTable;

        public override void OnCreate(object hook) {
            base.OnCreate(hook);
            _hook = hook as IXContainer;

            _IDisplayTable = _hook.Resolve<IDisplayTable>();
        }

        public override void OnClick(object parameter) {
            base.OnClick(parameter);
            HttpHelper http = new HttpHelper("http://syqgis.mwr.gov.cn:8080/syqMap//interestpoint/loadHydrologicalPointOfBBOXHydrologicalPointAction.action");
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("bottom", "71.188633");
            dic.Add("left", "-2.716211");
            dic.Add("top", "141.764805");
            dic.Add("right", "65.399023");
            dic.Add("level", "6");

            http.POSTAsync(AfterGetData, dic);
            this.IsEnabled = false;

            //_hook.Resolve<WebViewModel>().Url = "http://syqgis.mwr.gov.cn:8080/syqMap/";
        }

        private void AfterGetData(string responseStr) {
            //try {
            //    if (responseStr == null) {
            //        _IDisplayTable.DisplayData = null;
            //    }
            //    JArray nodes = JArray.Parse(responseStr); 
            //    List<SYQInfo> infos = new List<SYQInfo>();
            //    for (int i = 0; i < nodes.Count; i++) {
            //        JToken jToken = nodes[i];
            //        SYQInfo info = JsonConvert.DeserializeObject<SYQInfo>(jToken.ToString());
            //        infos.Add(info);
            //    }

            //    _IDisplayTable.DisplayData = infos.ToArray();
            //    _IDisplayTable.AddRowVisitor = new AddSYQInfo(_hook.Resolve<DbHelp.DbHelper>());
            //    _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + infos.Count.ToString() + " 条 水雨情 数据。");
            //}
            //catch (Exception ex) {
            //    CommonHelp.LogHelper.Error(ex);
            //}
            //finally {
            //    this.IsEnabled = true;
            //}
        }
    }
}
